from sqlalchemy import create_engine, Column, Integer, String, BigInteger, DateTime, ForeignKey, Float, TEXT
from sqlalchemy.orm import sessionmaker, declarative_base

import datetime

# 数据库配置信息
HOST = "rm-2zelg248g7y2034w7do.mysql.rds.aliyuncs.com"
USER ='sds_user'
PORT = 3306
PASSWORD = 'iscas0909'
DATABASE = 'sds_platform'

# 连接数据库
# 格式：dialect+driver://username:password@host:port/database
engine = create_engine('mysql+pymysql://{user}:{password}@{host}:{port}/{db_name}'.format(user=USER, password=PASSWORD, host=HOST, db_name=DATABASE, port=PORT))

# 创建 Session 类型
Session = sessionmaker(bind=engine)
# session = Session()

# 定义基类
Base = declarative_base()

# 对话表
class LLM_Catelog(Base):
    __tablename__ = 'LLM_Catelog' # 数据表名称

    id = Column(Integer, primary_key=True, autoincrement=True) # 
    role = Column(String(255)) # 角色
    uuid = Column(String(50)) # 真相源uuid
    content = Column(TEXT) # 对话内容
    time = Column(DateTime, default=datetime.datetime.utcnow) 

# 对话表
class LLM_Catelog_Func(Base):
    __tablename__ = 'LLM_Catelog_Func' # 数据表名称

    id = Column(Integer, primary_key=True, autoincrement=True) # 
    role = Column(String(255)) # 角色
    uuid = Column(String(50)) # 真相源uuid
    func_name = Column(String(100)) # 函数名称
    content = Column(TEXT) # 对话内容
    time = Column(DateTime, default=datetime.datetime.utcnow) 

# 对话表
class LLM_Catelog_Code_Exec_Analysis(Base):
    __tablename__ = 'LLM_Catelog_Code_Exec_Analysis' # 数据表名称

    id = Column(Integer, primary_key=True, autoincrement=True) # 
    role = Column(String(255)) # 角色
    uuid = Column(String(50)) # 真相源uuid
    content = Column(TEXT) # 对话内容
    time = Column(DateTime, default=datetime.datetime.utcnow) 


# 对话表
class LLM_Catelog_Code_Analysis(Base):
    __tablename__ = 'LLM_Catelog_Code_Analysis' # 数据表名称

    id = Column(Integer, primary_key=True, autoincrement=True) # 
    role = Column(String(255)) # 角色
    uuid = Column(String(50)) # 真相源uuid
    content = Column(TEXT) # 对话内容
    time = Column(DateTime, default=datetime.datetime.utcnow) 

# 创建数据表
Base.metadata.create_all(engine)

def auto_session(func):
    def wrapper(*args, **kwargs):
        session = Session()
        ret = func()(session, *args, **kwargs)
        session.commit()
        session.close()
        return ret
    return wrapper